package com.zkingsoft.actions.visitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.model.Message;
import com.zkingsoft.model.Nav;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.model.ShowElementCate;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.MeiduMessageAdapter;

/**
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年6月16日10:31
 * @description 美度前台action
 */
@RequestMapping(value = "/md")
@Controller
public class Meidu extends VistorBase {

	private static final Logger log = Logger.getLogger(Meidu.class);

	/**
	 * 进入首页
	 */
	@RequestMapping(value = "/index")
	public String index() {
		navQuery = new Nav();
		// 在项目中缓存导航栏信息
		if (getServletContext().getAttribute("navList") == null) {
			navQuery.setGrade(1);
			navQuery.setCompanyId(companyId);
			List<Nav> navList = navService.findByModel(navQuery);
			// 获取导航栏：这里后期看一下可以做优化不，在sql中一次性把子导航栏查出来
			for (Nav nav : navList) {
				navQuery = new Nav();
				navQuery.setParentId(nav.getId());
				List<Nav> childNav = navService.findByModel(navQuery);
				nav.setChildNav(childNav);
			}
			getServletContext().setAttribute("navList", navList);
		}

		setPointsByBoxToRequest("syggt", "首页广告图");

		setPointToRequestByName("jkfw", "全球WELLNESS健康服务");
		setPointsByBoxToRequest("WELLNESS", "WELLNESS");

		setPointToRequestByName("jphh", "精品护肤");
		setPointsByBoxToRequest("jphhs", "精品护肤");

		setPointToRequestByName("zyyq", "专业仪器");
		setPointsByBoxToRequest("zyyqs", "专业仪器");

		setPointToRequestByName("sssh", "三生生荟");
		setPointsByBoxToRequest("ssshs", "三生生荟");

		setPointToRequestByName("zxsc", "在线商城");
		setPointsByBoxToRequest("zxscs", "在线商城");

		return "meidu/index";
	}

	/**
	 * 进入集团简介
	 */
	@RequestMapping(value = "/introduction")
	public String aboutus() {
		List<ShowElement> arts = new ArrayList<>();
		arts.add(setShowElementToRequestByName("jt", "集团简介"));
		arts.add(setShowElementToRequestByName("fzlc", "发展历程"));
		arts.add(setShowElementToRequestByName("qyjs", "企业精神"));
		getRequest().setAttribute("arts", arts);
		return "meidu/introduction";
	}

	/**
	 * 进入企业集团
	 */
	@RequestMapping(value = "/company")
	public String company(Long id) {
		showElementCateQuery = new ShowElementCate();
		showElementCateQuery.setName("集团业务总览");
		showElementCateQuery.setCompanyId(companyId);
		List<ShowElementCate> list = showElementCateService.findByModel(showElementCateQuery);
		if (list.size() > 0) {
			ShowElementCate parentCate = list.get(0);
			showElementCateQuery = new ShowElementCate();
			showElementCateQuery.setParentId(parentCate.getId());
			List<ShowElementCate> childCate = showElementCateService.findByModel(showElementCateQuery);
			// 查询出分类里面所有的文章
			for (ShowElementCate showElementCate : childCate) {
				showElementQuery = new ShowElement();
				showElementQuery.setCateId(showElementCate.getId());
				showElementCate.setShowelemets(showElementService.findByModel(showElementQuery));
			}
			// 查询出要显示的文章
			ShowElement art = showElementService.findById(id);
			getRequest().setAttribute("cates", childCate);
			getRequest().setAttribute("currentArt", art);
		}
		return "meidu/company";
	}

	/**
	 * 进入行业影响
	 */
	@RequestMapping(value = "/influences")
	public String influences() {
		setShowElementToRequestByName("hyyx", "行业影响");
		return "meidu/influences";

	}

	/**
	 * 进入集团资讯
	 */
	@RequestMapping(value = "/news")
	public String news() {

		int time = 2016;
		HashMap<String, Object> query = new HashMap<>();
		query.put("beginTime", DateUtils.stringToDate(time + "", "yyyy"));
		query.put("endTime", DateUtils.stringToDate(time + 1 + "", "yyyy"));
		query.put("cateName", "集团资讯");
		query.put("companyId", companyId);
		query.put("begin", 0);
		query.put("end", 9);

		List<ShowElement> elements = showElementService.findByTime(query);
		getRequest().setAttribute("arts", elements);
		getRequest().setAttribute("currentTime", "2016");
		getRequest().setAttribute("pageNow", 1);
		return "meidu/news";
	}

	/**
	 * 根据时间加载后9篇文章
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/morenews")
	public @ResponseBody AjaxResult getChildNav(int time, int pageNow) {
		HashMap<String, Object> query = new HashMap<>();
		query.put("beginTime", DateUtils.stringToDate(time + "", "yyyy"));
		query.put("endTime", DateUtils.stringToDate(time + 1 + "", "yyyy"));
		query.put("cateName", "集团资讯");
		query.put("companyId", companyId);
		query.put("begin", pageNow * 9);
		query.put("end", 9);

		List<ShowElement> elements = showElementService.findByTime(query);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, elements, 0, 0);
		Map<String, Object> map = new HashMap<>();
		map.put("pageNow", pageNow + 1);
		map.put("currentTime", time);
		return result;

	}

	/**
	 * 进入人才招聘
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/offer")
	public String offer() {
		setShowElementToRequestByName("zxzw", "最新的职位信息");
		setShowElementToRequestByName("yggh", "员工关怀与福利");
		return "meidu/offer";

	}

	/**
	 * 进入联系我们
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/connectus")
	public String connectus() {
		return "meidu/connectus";
	}
	
	
	
	
	
	/**
	 * 添加留言
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/addMessage")
	public String addMessage(HttpServletRequest request, MeiduMessageAdapter message) {
		
		Message mdMessage=new Message();
		mdMessage.setAddress(message.getAddress()+message.getArea());
		mdMessage.setContent(message.getContent());
		mdMessage.setIp(message.getEmail());
		mdMessage.setUser(message.getLastName()+message.getFirstName()+":"+message.getTitle());
		mdMessage.setTel(message.getTel());
		mdMessage.setTime(new Date());
		mdMessage.setCompanyId(companyId);
		messageService.add(mdMessage);
		request.setAttribute("msg", "感谢您的留言，我们会尽快处理");
		return "meidu/connectus";
	}
	
	
	
	
	
	
	
	

}